package com.byyy.ccts.modules.sys.controller;

import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.core.utils.CacheUtils;
import com.byyy.ccts.modules.sys.entity.DictGroup;
import com.byyy.ccts.modules.sys.service.IDictGroupService;
import com.byyy.ccts.modules.sys.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Controller
@RequestMapping("${admin.url.prefix}/sys/dict/group")
@RequiresPathPermission("sys:dict")
public class DictGroupController extends BaseCRUDController<DictGroup, String> {

	@Autowired
	private IDictGroupService dictGroupService;

	@RequestMapping(value = "/forceRefresh", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson forceRefresh(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.success("字典刷新成功");
		DictUtils.clear();
		return ajaxJson;
	}

	/**
	 * 更新停用状态
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateState", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson updateState(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.success("更新完毕");
		String[] ids = request.getParameterValues("ids");
		String state = request.getParameter("state");
		this.dictGroupService.updateState(Arrays.asList(ids), state);
		return ajaxJson;
	}

	/**
	 * 清除缓存
	 * @return
	 */
	@RequestMapping(value = "/clearCache", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson clearCache() {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.success("缓存清除完毕");
		CacheUtils.getCacheManager().clearAll();
		return ajaxJson;
	}

}
